package com.xiang.activity;

import com.xiang.async.AsynImageLoader;
import com.xiang.constant.DealBaseInfo;
import com.xiang.seemovies.R;
import com.xiang.utils.ActivityJumpUril;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.fzxy.zxy.niceday.examples.BaseActivity;

public class DealDetailActivity extends BaseActivity {
	private TextView title;
	private TextView price;
	private TextView date;
	private TextView description;
	private Button toPage;
	private ImageView img;
	private DealBaseInfo baseinfo;
	private ImageView back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.deal_detail_layout);
		initData();
		initView();
	}

	private void initData() {
		Bundle bundle = getIntent().getBundleExtra("bundle");
		baseinfo = (DealBaseInfo) bundle.get("dealBaseInfo");
	}

	private void initView() {
		title = (TextView) findViewById(R.id.deal_detail_title);
		price = (TextView) findViewById(R.id.deal_detail_price);
		date = (TextView) findViewById(R.id.deal_detail_data);
		description = (TextView) findViewById(R.id.deal_detail_description);
		toPage = (Button) findViewById(R.id.btn_to_htm5);
		back = (ImageView) findViewById(R.id.deal_detail_back);
		img = (ImageView) findViewById(R.id.deal_detail_img);
		if (baseinfo != null) {
			title.setText(baseinfo.title);
			price.setText("价格：￥"+baseinfo.current_price);
			date.setText("截止购买日期："+baseinfo.purchase_deadline);
			description.setText(baseinfo.description);
			
			AsynImageLoader mImageAsynLoader = new AsynImageLoader();
			Bitmap cachedImage = mImageAsynLoader.loadDrawableFromNet(img, baseinfo.s_image_url); 
			img.setImageBitmap(cachedImage);
		}
		toPage.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityJumpUril.detailToHtml5Page(DealDetailActivity.this, baseinfo.deal_h5_url);
				overridePendingTransition(R.anim.right_in, R.anim.left_out);//打开新界面
			}
		});
		back = (ImageView) findViewById(R.id.deal_detail_back);
		back.setOnClickListener(new ImageView.OnClickListener() {
			@Override
			public void onClick(View v) {
				DealDetailActivity.this.finish();
				overridePendingTransition(R.anim.left_in, R.anim.right_out);
			}
		});
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
        	DealDetailActivity.this.finish();
        	overridePendingTransition(R.anim.left_in, R.anim.right_out);
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }
}
